package com.idlecode.config.query;

import cn.hutool.core.util.StrUtil;
import com.idlecode.config.DbType;

/**
 * 达梦8 查询
 */
public class DmQuery implements AbstractQuery {

    @Override
    public DbType dbType() {
        return DbType.DM;
    }

    @Override
    public String dbNamesSql() {
        return "select distinct OWNER from dba_tables order by OWNER";
    }

    @Override
    public String tableSql(String dataBaseName, String tableName) {
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT T.* FROM (");
        sql.append("SELECT T1.TABLE_NAME AS TABLE_NAME, T2.COMMENTS AS TABLE_COMMENT FROM ALL_TABLES T1 LEFT JOIN ALL_TAB_COMMENTS T2 ON T1.TABLE_NAME = T2.TABLE_NAME and T1.OWNER = T2.OWNER");
        sql.append(" where 1=1 ");
        //数据库查询
        if (StrUtil.isNotBlank(dataBaseName)) {
            sql.append("and T1.OWNER='").append(dataBaseName).append("'");
        }
        sql.append(") T WHERE 1=1 ");
        // 表名查询
        if (StrUtil.isNotBlank(tableName)) {
            sql.append("and T.TABLE_NAME = '").append(tableName).append("' ");
        }
        sql.append("order by T.TABLE_NAME asc");

        return sql.toString();
    }

    @Override
    public String dbName() {
        return "OWNER";
    }

    @Override
    public String tableFieldsSql(String dataBaseName, String tableName) {
        return
                StrUtil.format("select\n" +
                        "  distinct COLUMN_NAME,\n" +
                        "  COMMENTS,\n" +
                        "  DATA_TYPE,\n" +
                        "  DATA_PRECISION,\n" +
                        "  DATA_SCALE,\n" +
                        "  KEY\n" +
                        "from\n" +
                        "  (\n" +
                        "    SELECT\n" +
                        "      T1.COLUMN_NAME,\n" +
                        "      T2.COMMENTS,\n" +
                        "      T1.DATA_TYPE,\n" +
                        "      T1.DATA_PRECISION,\n" +
                        "      T1.DATA_SCALE,\n" +
                        "      CASE WHEN CONSTRAINT_TYPE = 'P' THEN 'PRI' END AS KEY\n" +
                        "    FROM\n" +
                        "      ALL_TAB_COLUMNS T1\n" +
                        "      LEFT JOIN ALL_COL_COMMENTS T2 ON T1.TABLE_NAME = T2.TABLE_NAME\n" +
                        "      AND T1.COLUMN_NAME = T2.COLUMN_NAME\n" +
                        "      AND T1.OWNER = T2.SCHEMA_NAME\n" +
                        "      LEFT JOIN (\n" +
                        "        SELECT\n" +
                        "          T4.TABLE_NAME,\n" +
                        "          T4.COLUMN_NAME,\n" +
                        "          T4.OWNER,\n" +
                        "          T5.CONSTRAINT_TYPE\n" +
                        "        FROM\n" +
                        "          ALL_CONS_COLUMNS T4,\n" +
                        "          ALL_CONSTRAINTS T5\n" +
                        "        WHERE\n" +
                        "          T4.CONSTRAINT_NAME = T5.CONSTRAINT_NAME\n" +
                        "          AND T4.OWNER = T5.OWNER\n" +
                        "          AND T5.CONSTRAINT_TYPE = 'P'\n" +
                        "      ) T3 ON T1.TABLE_NAME = T3.TABLE_NAME\n" +
                        "      AND T1.COLUMN_NAME = T3.COLUMN_NAME\n" +
                        "      AND T1.OWNER = T3.OWNER\n" +
                        "    WHERE\n" +
                        "      T1.OWNER = '{}'\n" +
                        "      AND T1.TABLE_NAME = '{}'\n" +
                        "    ORDER BY\n" +
                        "      T1.TABLE_NAME,\n" +
                        "      T1.COLUMN_ID\n" +
                        "  )", dataBaseName, tableName);
    }

    @Override
    public String tableName() {
        return "TABLE_NAME";
    }

    @Override
    public String tableComment() {
        return "TABLE_COMMENT";
    }

    @Override
    public String tableUpdateTime() {
        return "UPDATE_TIME";
    }

    @Override
    public String fieldName() {
        return "COLUMN_NAME";
    }

    @Override
    public String fieldType() {
        return "DATA_TYPE";
    }

    @Override
    public String fieldComment() {
        return "COMMENTS";
    }

    @Override
    public String fieldKey() {
        return "KEY";
    }

    @Override
    public String dataPrecision() {
        return "DATA_PRECISION";
    }

    @Override
    public String dataScale() {
        return "DATA_SCALE";
    }
}
